/*
ID: saboia
LANG: C++
TASK: digits
*/
//WA
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <iostream>
#include <list>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;


int main (){
	freopen("digits.in", "r", stdin);freopen("digits.out", "w", stdout);
	//freopen("in.txt", "r", stdin);
	char a[100], b[100], n, m, i, j, k, s, ok, p;
	vector<int> v;
	scanf("%s%s", a, b);
	n=strlen(a);
	m=strlen(b);
	for(i=n-1;i>=0;--i){
		k=1;
		s=0;
		for(j=n-1;j>=0;--j, k*=2){
			if(j==i)s+= (!(a[j]-'0'))*k;
			else s+= (a[j]-'0')*k;
		}
		v.push_back(s);
		//printf("%d %d\n", s, n);
	}
	ok=1;
	for(p=1;p<3;++p)
	for(i=m-1;i>=0&&ok;--i){
		k=1;
		s=0;
		for(j=m-1;j>=0;--j, k*=3){
			if(j==i){
				s+= (((b[j]-'0')+p)%3)*k;
			}
			else{
				s+= (b[j]-'0')*k;
			}
		}
		//printf("%d %d\n", s, m);
		for(j=0;j<v.size();++j){
			if(v[j]==s)ok=0;
		}
	}
	
	printf("%d\n", s);
	
	return 0;
}
